package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.ChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.ChannelService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChannelserviceImpl extends ServiceImpl<ChannelMapper, WmChannel> implements ChannelService {

    @Autowired
    private WmNewsMapper wmNewsMapper;


    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult channelDelete(Integer id) {
        WmChannel byId = this.getById(id);
        if (byId.getStatus()) {
            return ResponseResult.errorResult(501, "启用状态无法删除");

        }
        this.removeById(id);
        return ResponseResult.okResult(200, "删除成功");
    }

    /**
     * 频道名称模糊分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageFind(ChannelDto dto) {
        Page<WmChannel> page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getName())) {
            lambdaQueryWrapper.like(WmChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {
            lambdaQueryWrapper.eq(WmChannel::getStatus, dto.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page(page, lambdaQueryWrapper);
        if (page == null) {
            return ResponseResult.okResult(page);
        }
        return ResponseResult.okResult(page.getRecords());
    }

    /**
     * 新增保存
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult channelSave(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmChannel::getName, wmChannel.getName());
        WmChannel one = this.getOne(lambdaQueryWrapper);
        if (one != null) {
            return ResponseResult.errorResult(501, "此频道已存在");
        }
        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);
        return ResponseResult.okResult(200, "新增成功");
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult channelUpdate(WmChannel wmChannel) {
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId());
        List<WmNews> wmNews = wmNewsMapper.selectList(wrapper);
        if (!wmNews.isEmpty() && !wmChannel.getStatus()) {
            return ResponseResult.errorResult(503,"被引用的频道不能禁用");
        }
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(WmChannel::getId,wmChannel.getId());
        this.update(wmChannel,lambdaQueryWrapper);
        return ResponseResult.okResult(200,"修改成功");
    }
}
